from pymodbus.client.sync import ModbusSerialClient as ModbusClient

class TPCS:
    def __init__(self):
        #feature
        self.config = {
            "modbusRtu":{
                "slaveId":1 ,
                "com":"COM3",
                "bRate": 9600
            },
            "mq":{
                "host":"",
                "port":"",
                "uid":"",
                "rootTopic":"xz2024/root",
            },
            "tdb":{
                "host":"",
                "port":"",
                "uid":"",
                "pwd":"",
                "db":"tdbPcs"
            }
        }
        #object
        self.modubusClient = None
        self.tdbClient = None
        self.mqttClient = None
        
    #status
    def getModbusConnected(self):
        res = False
        try:
            res = self.modubusClient !=None
        except Exception as er :
            print(er)
        return  res
    def getMqttConnected(self):
        res = False
        try:
            pass
        except Exception as er :
            print(er)
        return  res
    def getTDBConnected(self):
        res = False
        try:
            pass
        except Exception as er :
            print(er)
        return  res
    def canSendNotify(self):
        res = False
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
        return  res
    def connect2Modbus(self):
        try:
            self.modubusClient = ModbusClient(
                method='rtu',
                port=self.config['modbusRtu']['COM'],
                baudrate=self.config['modbusRtu']['bRate'])
            self.modubusClient.connect()
        except Exception as er :
            self.modubusClient = None
            print(er)
    def connect2Tdb(self):
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
    def connect2MqServer(self):
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
    def readRegistInfo(self):
        registers = []
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
        return  registers
    def decode(self, regsiterValues):
        vars =[]
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
        return  vars
    def save2TDB(self ,vars):
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
    #method
    def notify(self,vars):
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)
    #method
    def start(self):
        try:
            self.connect2Modbus()
            self.connect2Tdb()
            self.connect2MqServer()

            while 1>0 :
                try:
                    regsiterValues = self.readRegistInfo()
                    vars = self.decode(regsiterValues)
                    self.save2TDB( vars)
                    self.notify(vars)
                except Exception as eer:
                    print(eer)
                    break
            self.stop()
        except Exception as er :
            print(er)
    def stop(self):
        try:
            res = self.mqttClient!=None and self.getMqttConnected()
        except Exception as er :
            print(er)


    #events

pcs = TPCS()
if __name__ == '__main__':
    print('xz2024')
    print(pcs.config['mq']['rootTopic'])
